package com.fyp.client.ptma.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.fyp.client.ptma.R;
import com.fyp.client.ptma.model.ApplicationVersion;
import com.fyp.client.ptma.model.Defect;

public class CustomAdapterLog extends BaseAdapter
{
	private LayoutInflater mInflater;
	private ApplicationVersion appVersion;

	public CustomAdapterLog(Context ctx)
	{
		mInflater = LayoutInflater.from(ctx);
	}

	public ApplicationVersion getData()
	{
		return this.appVersion;
	}

	@Override
	public int getCount()
	{
		return appVersion.getDefects().size();
	}

	public void setData(ApplicationVersion appVersion)
	{
		this.appVersion = appVersion;
	}

	@Override
	public Object getItem(int arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0)
	{
		// TODO Auto-generated method stub
		return 0;
	}

	private static class ViewHolder
	{

		private TextView tvLogTime;
		private TextView tvLog;
		private TextView tvDefectId;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup group)
	{
		Defect defect = appVersion.getDefects().get(position);

		final ViewHolder holder;
		if (convertView == null)
		{

			if (defect.getCommenterCode().equals(Defect.DEVELOPER))
			{
				convertView = mInflater.inflate(R.layout.log_row_dev, null);
			} else
			{
				convertView = mInflater.inflate(R.layout.log_row_client, null);
			}
			holder = new ViewHolder();

			// Dev vars
			holder.tvLogTime = (TextView) convertView
					.findViewById(R.id.tvLogTime);
			holder.tvLog = (TextView) convertView.findViewById(R.id.tvLog);
			holder.tvDefectId = (TextView) convertView
					.findViewById(R.id.tvDefectId);

			convertView.setTag(holder);
		} else
		{

			holder = (ViewHolder) convertView.getTag();
		}

		holder.tvLog.setText(defect.getComment());
		holder.tvLogTime.setText(defect.getStringTimeStamp());
		holder.tvDefectId.setText("Defect Id:" + defect.getId());

		return convertView;

	}
}
